
#include "C:\SAFIRE\Y\Y_BIFn\Y_BIFn.y.h"

#include "C:\SAFIRE\Y\PH_SL.y.h"

#include "C:\SAFIRE\Y\Mgt_SL.y.h"

#include "C:\SAFIRE\Y\ConfigUdp_TP.y.h"

#include "C:\SAFIRE\Users\SIP_USER\ws\SIP\source\Common_LIB\Scenario_HTML_Gen.y.h"

#include "C:\SAFIRE\USERS\SIP_USER\ws\SIP\source\SIP_LIB\SIP_LIB.y.h"

#include "C:\SAFIRE\USERS\SIP_USER\ws\SIP\source\SIP_T_LIB\SIP_T_LIB.y.h"


typedef SIP_T_ST_CC_OE_CE_V [SIP_Signals_T SIP_G, PH_SL UDP_Data_G, Mgt_SL UDP_Mgt_G]
{
            
    TIMER SIGNAL        TSupervisor();
    TIMER SIGNAL        TAct_Rq();
    TIMER SIGNAL        TResponse();
    TIMER SIGNAL        TStart();
	
       
    Request_T           Request_TX_1;
    Request_T           Request_TX_2;
    Request_T           Request_RX_1;
    Request_T           Request_RX_2;
    
    Response_T          Response_T_TX_1;
    Response_T          Response_T_TX_2;
    Response_T          Response_T_RX_1;
    Response_T          Response_T_RX_2;
    
    Request_Line_T      Request_Line;
    Message_Headers_T   Message_Headers;
    Message_Body_T      Message_Body;
    Status_Line_T       Status_Line;
    
    PH_SL               PH_SL_S;
    SIP_Signal_T        SIP_Signal;    
    Config_UDP_T        ConfigUDP;
    
    STRING              CRLF;
        
    STRING              ScenarioName;
    STRING              ScenarioDecription;
    STRING              HTML_Title;
    STRING              ScenarioVerdict;
    STRING              ScenarioReason;    
    STRING              HTMLFilePath;
    STRING              Mode;
    NUMBER              HTML_File;
	
	SIP_T_Body_T		SIP_T_Body_IAM;
	SIP_T_Body_T		SIP_T_Body_ACM;	
	SIP_T_Body_T		SIP_T_Body_ANM;
	SIP_T_Body_T		SIP_T_Body_RLC;	
	SIP_T_Body_T		SIP_T_Body_REL;	
	
	Request_Line_T 		Request_Line;
	
	SIP_Addr_Spec_T		SUT_Addr;
	SIP_Addr_Spec_T		Tester_Addr;
	
    SIP_T_Body_IAM = ConvertFromHex( "1501010020010A00020A08819050608006000F0A08041779516028223000");
	SIP_T_Body_ACM = ConvertFromHex( "AA000616040129010100");
	SIP_T_Body_ANM = ConvertFromHex( "F0010900");
	SIP_T_Body_RLC = ConvertFromHex( "7D021000");
	SIP_T_Body_REL = ConvertFromHex( "75000C0200028090");	
	
  
    SUT_Addr.SIP_URI.UserInfo.User = "228";
	SUT_Addr.SIP_URI.UserInfo.Password = NULL;	
	SUT_Addr.SIP_URI.HostPort.Host = "127.0.0.1";
	SUT_Addr.SIP_URI.HostPort.Port = 5060;	
	SUT_Addr.SIP_URI.URI_Parameters = NULL;
	SUT_Addr.SIP_URI.URI_Headers = NULL;
	
    Tester_Addr.SIP_URI.UserInfo.User = "1000";
	Tester_Addr.SIP_URI.UserInfo.Password = NULL;	
	Tester_Addr.SIP_URI.HostPort.Host = "127.0.0.1";
	Tester_Addr.SIP_URI.HostPort.Port = 5061;	
	Tester_Addr.SIP_URI.URI_Parameters = NULL;
	Tester_Addr.SIP_URI.URI_Headers = NULL;
		
    
    Mode= "ab";
    HTMLFilePath= "C:\\SAFIRE\\Temp\\SIP_ST_CC_OE_CE_V.html";    
    
    HTML_File= OpenFile( HTMLFilePath,
                         Mode);
                             
    HTML_Title = "SIP_ST_CC_OE_CE_V";
    ScenarioName = "SIP_ST_CC_OE_CE_V";
    ScenarioDecription = "basic call - passive part";
    Scenario_Gen_HTML_Header( HTML_File,
                              HTML_Title,
                              ScenarioName,
                              ScenarioDecription);

    START TStart FOR 1000;
    Print("\n\nScenario_1 : ---  started");
    
    ? .TStart()
    {
         Print("\n\nScenario_1 : ---  started");
         :: Preamble;
    }        
     
    
    STATE Preamble
    {        
        Print("SIP_T_ST_CC_OE - Start UDP config");
        ConfigUDP.Src.IpAddress=        Tester_Addr.SIP_URI.HostPort.Host;
        ConfigUDP.Src.Port=             Tester_Addr.SIP_URI.HostPort.Port;
        
        ConfigUDP.Dst.IpAddress=        SUT_Addr.SIP_URI.HostPort.Host;
        ConfigUDP.Dst.Port=             SUT_Addr.SIP_URI.HostPort.Port;
        
        ConfigUDP.MaxPacketSize=        1024;
        Print("SIP_ST_CC_OE - End UDP config");

        Print("SIP_ST_CC_OE - Configuring UDP");
        UDP_Mgt_G ! Mgt_SL.Configure( ConfigUDP);
        
        Print("SIP_ST_CC_OE - Activating UDP");
        UDP_Data_G ! PH_SL.PH_Act_Rq();
        START TAct_Rq FOR 10000;
    
        UDP_Data_G ? PH_SL.PH_Act_Ind() : PH_SL_S 
        {
            Print( "SIP_ST_CC_OE - UPD Activated");
            START TSupervisor FOR 10000;
            
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       0, 
                                       "PASS",
                                       "UDP activated");
            :: Starting;
        }
        
        UDP_Data_G ? PH_SL.PH_Deact_Ind() : PH_SL_S 
        {
            Print( "SIP_ST_CC_OE - UPD Act_Rq not successfull  ... PH_Deact_Ind received!");
            
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "UPD Act_Rq not successfull  ... PH_Deact_Ind received!");            
            STOP;
        }
        
        ? .TActInd()
        {
            Print( "SIP_ST_CC_OE - UPD Act_Req not received  ... Timeout!");
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "UPD Act_Req not received  ... timeout!");
                                       
            STOP;
        }                
    }             
     
    STATE Starting
    {            
		Print("SIP_ST_CC_OE - Starting");
		
		START TStart FOR 10000;
		
		? .TStart()
		{
            Print( "SIP_ST_CC_OE - No call started  ... Timeout!");
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "No call started  ... timeout!"); 
            STOP;
        }
		
        SIP_G ? SIP_Signals_T.SIP_Invite (?) : SIP_Signal
		{
			Print("SIP_ST_CC_OE - SIP_Invite message received");
			Print(SIP_Signal);
			            
            Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "Tester received INVITE from SUT with SIP-T Message-Body");
			
			Status_Line.SIP_Version = "SIP/2.0";
			Status_Line.Status_Code = 100;
			Status_Line.Reason_Phrase.Token_List = "Trying";
            
			Message_Headers.Via.Via_Param_List.Via_Param.Sent_Protocol.Token = "SIP/2.0/UDP";
            Message_Headers.Via.Via_Param_List.Via_Param.Sent_by.HostPort.Host = Tester_Addr.SIP_URI.HostPort.Host;
			Message_Headers.Via.Via_Param_List.Via_Param.Sent_by.HostPort.Port = Tester_Addr.SIP_URI.HostPort.Port;
			Message_Headers.Via.Via_Param_List.Via_Param.Via_Params_List.Token = "branch=z9hG4bKkdjuw2212";
			Message_Headers.From.From_Spec.Addr.Name_Addr.Addr_Spec = Tester_Addr;
			Message_Headers.From.From_Spec.From_Param_List.Token = "tag=a3bhg";
			Message_Headers.To.To_Spec.Addr.Addr_Spec = SUT_Addr;
			Message_Headers.Call_Id.Callid.Token = "c3eF6f58-363323Cd-8058EB88-10306184";
			Message_Headers.Call_Id.Callid.Host = Tester_Addr.SIP_URI.HostPort.Host;
			Message_Headers.CSeq.Cseq_Number = 1;
			Message_Headers.CSeq.Method.Invite.MustBeNull = NULL;
			Message_Headers.User_Agent.Server_Val_List.Token_List = "Asterisk PBX";
			Message_Headers.Allow.Method_List.Token_List = "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY";
			Message_Headers.Contact.Contact_Spec.Contact_Param_List.Contact_Param.Addr.Addr_Spec = Tester_Addr;
            
			Response_T_TX_1.Status_Line = Status_Line;
			Response_T_TX_1.Message_Headers = Message_Headers;			
            SIP_G ! SIP_Signals_T.SIP_100_Trying (Response_T_TX_1);
            Print("SIP_ST_CC_OE - SIP_100_Trying sent");
            
			Response_T_TX_1.Status_Line.Status_Code = 180;
			Response_T_TX_1.Status_Line.Reason_Phrase.Token_List = "Ringing";			
			SIP_G ! SIP_Signals_T.SIP_180_Ringing (Response_T_TX_1);
            Print("SIP_ST_CC_OE - SIP_180_Ringing sent");
            
            Response_T_TX_1.Status_Line.Status_Code = 200;
			Response_T_TX_1.Status_Line.Reason_Phrase.Token_List = "OK";
			
			Response_T_TX_1.Message_Body = SIP_T_Body_ANM;
            
            SIP_G ! SIP_Signals_T.SIP_200_OK (Response_T_TX_1);
            Print("SIP_ST_CC_OE - SIP_180_Ringing sent");
			
            START TResponse FOR 5000;
			:: CallEnd;
		}
		

    }

    STATE CallEnd
    {        
        SIP_G ? SIP_Signals_T.SIP_Ack (Request_TX_1) : SIP_Signal 
        {
            Print("SIP_ST_CC_OE - VERDICT: PASS! - SIP_Ack received");
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       0, 
                                       "PASS",
                                       "SIP_Cancel received");                  
        }       
        
        SIP_G ? SIP_Signals_T.SIP_Cancel (Request_TX_1) : SIP_Signal 
        {
            Print("SIP_ST_CC_OE - VERDICT: PASS! - SIP_Cancel received");
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "PASS",
                                       "SIP_Cancel received");
            
            :: Postamble;       
        }
        
        SIP_G ? ? : SIP_Signal 
        {                
            Print("SIP_ST_CC_OE - VERDICT: FAIL! - Unexpected message received");
            Print(SIP_Signal);
                                
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "Unexpected message received");                                 
            :: Postamble;
        }
        
     
        ? .TResponse()
        {
            Print("SIP_ST_CC_OE - VERDICT: FAIL! - TResponse timer expiered");
                                           
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "TResponse timer expiered");                                  
                                
            :: Postamble;
        }
        
        ? .TSupervisor()
        {
            Print("SIP_ST_CC_OE - VERDICT: FAIL! - TSupervisor timer expiered");
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "TSupervisor timer expiered");
            
            :: Postamble;
        }
    }
    
    STATE Postamble
    {
        Print("SIP_ST_CC_OE - ... ending the test scenario");
        UDP_Data_G ! PH_SL.PH_Deact_Rq();
        Scenario_Gen_HTML_Footer( HTML_File);
        CloseFile( HTML_File);
        STOP;                
    }
};